<?php
// +----------------------------------------------------------------------
// | 方维购物分享网站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://fanwe.com All rights reserved.
// +----------------------------------------------------------------------

/**  
 * payment.service
 *
 * 充值服务类
 *
 * @package service
 * @author awfigq <awfigq@qq.com>
 */
class PaymentService
{
	public function getPaymentList()
	{
		$res = DB::query('SELECT id,logo,name,description,fee,fee_type FROM '.DB::table('payment').' WHERE status = 1 ORDER BY sort ASC,id ASC');
		$list = array();
		while($payment = DB::fetch($res))
		{
			$payment['fee'] = floatval($payment['fee']);
			if($payment['fee_type'] == 0)
				$payment['fee_format'] = "¥".$payment['fee'];
			else
				$payment['fee_format'] = $payment['fee']."%";
				
			$list[] = $payment;
		}
		
		return $list;
	}
	
	//获取订单的支付接口
	function getPayment($rec_id,$amount=0)
	{		
		$incharge_info = DB::fetchFirst('SELECT id,payment,money FROM '.DB::table('user_incharge').' WHERE id='.$rec_id);
		
		$payment_info = DB::fetchFirst('SELECT id, currency,fee_type,fee,online_pay,class_name,name FROM '.DB::table('payment').' where id='.intval($incharge_info['payment']));
		
		$money = $incharge_info['money'];
		
		if($payment_info['fee_type'] == 0)
			$money = $money + $payment_info['fee'];
		else 
			$money = $money + ($money * $payment_info['fee'] / 100);
		
		$payment_id = $payment_info['id'];
		
		require FANWE_ROOT.'./ThinkPHP/Vendor/payment/'.$payment_info['class_name'].'Payment.class.php';
		$payment_class = $payment_info['class_name']."Payment";
		$payment_model = new $payment_class;
		
		$code =  $payment_model->getPaymentCode($rec_id, $money, $payment_id);
		return $code;
	}
	
	public function paymentPaid($rec_id, $money, $payment_id)
	{
		global $_FANWE;
        $result  =  array();
        $result['order_id'] = 0;
		
		$incharge = DB::fetchFirst('SELECT id,is_paid,payment,user_id,money FROM '.DB::table('user_incharge').' WHERE id='.$rec_id);
    	
    	if ($incharge === false)
		{
			$result['status'] = false;
        	$result['info'] = '无效支付ID'.$rec_id;
        	$result['data'] = '无效支付ID'.$rec_id;        	
    		return $result;    		
    	}
    	
        if ($incharge['is_paid'] == 1)
		{
			$result['status']  =  true;
        	$result['info'] = '支付单号ID：'.$rec_id.'  支付金额：¥'. $money .'支付成功';
        	$result['data'] = '支付单号ID：'.$rec_id.'  支付金额：¥'. $money .'支付成功';
    		return $result;
    	}  	

    	/* 修改此次支付操作的状态为已付*/
		$updates['is_paid'] = 1;
		$updates['update_time'] = TIME_UTC;
		$updates['status'] = 1;
		DB::update('user_incharge',$updates,'id = '.$rec_id);
		
		$rs = DB::affectedRows();
		if($rs == 0) //添加判断，防止重复收款
			return '支付单号ID：'.$rec_id.'已经失效';
		
		$ratio = $_FANWE['setting']['money_score_ratio'];
		$score = $incharge['money'] * $ratio;
		$status = S('User')->updateUserScore($incharge['user_id'], 'User', 'Incharge', '在线充值兑换积分',$rec_id, $score);
    	
		if ($status === true)
		{
			$result['status']  =  true;
        	$result['info'] = '支付单号ID：'.$rec_id.'  支付金额：¥'. $money .'支付成功';
        	$result['data'] = '支付单号ID：'.$rec_id.'  支付金额：¥'. $money .'支付成功';
		}
		else
		{
			$result['status'] = false;
        	$result['info'] = $status;
        	$result['data'] = $status;			
		}
		
		return $result;
	}
}
?>